package org.opsli.modulars.mini;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.opsli.api.base.result.ResultWrapper;
import org.opsli.api.web.shop.previousevents.ShopPreviousEventsRestApi;
import org.opsli.api.web.shop.shopconfig.ShopConfigRestApi;
import org.opsli.api.web.shop.userorder.ShopUserOrderRestApi;
import org.opsli.api.web.system.options.OptionsApi;
import org.opsli.api.web.system.org.SysOrgRestApi;
import org.opsli.api.wrapper.shop.previousevents.ShopPreviousEventsModel;
import org.opsli.modulars.weixinPay.Confiig.WxPayConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Api(tags = ShopConfigRestApi.TITLEWECHATMINI)
@Slf4j
@RestController
@RequestMapping("/weMini/NotToken")
public class indexRestControlle {
    @Resource
    WxPayConfiguration wxPayConfiguration;
    @Autowired
    ShopPreviousEventsRestApi shopPreviousEventsRestApi;
    @Autowired
    ShopUserOrderRestApi shopUserOrderRestApi;
    @Autowired
    SysOrgRestApi sysOrgRestApi;
    @Autowired
    private OptionsApi optionsApi;
    @Autowired
    private WxMaService wxMaService;

    @GetMapping("/getoptionsList")
    @ApiOperation(value = "获得单条店铺配置", notes = "获得单条店铺配置 - ID")
    public ResultWrapper<?> get() {
        // 如果系统内部调用 则直接查数据库
        Map<String, Object> map = new HashMap<>();
        map.put("miniappconfiggundonglan", optionsApi.getByCode("miniappconfiggundonglan").getData());
        map.put("gonggaoindex", optionsApi.getByCode("gonggaoindex").getData());
        map.put("gonggao2", optionsApi.getByCode("gonggao2").getData());
        map.put("viplist", optionsApi.getByCode("vipList").getData());
        map.put("wangqihuodong", shopPreviousEventsRestApi.getList().getData());
        map.put("orgList", sysOrgRestApi.orgList().getData());
        return ResultWrapper.getSuccessResultWrapper(map);
    }

    @GetMapping("/getpreviouseventsbyid")
    @ApiOperation(value = "获得单条店铺配置", notes = "获得单条店铺配置 - ID")
    public ResultWrapper<?> getpreviouseventsbyid(String id) {
        // 如果系统内部调用 则直接查数据库
        ShopPreviousEventsModel model = new ShopPreviousEventsModel();
        model.setId(id);
        model.setIzApi(true);
        return ResultWrapper.getSuccessResultWrapper(shopPreviousEventsRestApi.get(model).getData());
    }


    @GetMapping("/getpreviouseventsbyListorgid")
    @ApiOperation(value = "获得单条店铺配置", notes = "获得单条店铺配置 - ID")
    public ResultWrapper<?> getpreviouseventsbyListid(String id) {
        // 如果系统内部调用 则直接查数据库
        ShopPreviousEventsModel model = new ShopPreviousEventsModel();
        model.setId(id);
        model.setIzApi(true);
        return ResultWrapper.getSuccessResultWrapper(shopPreviousEventsRestApi.getList(id).getData());
    }


    @ApiOperation(value = "支付回调通知处理")
    @PostMapping("/notify/order")
    public String parseOrderNotifyResult(@RequestBody String xmlData) throws WxPayException {
        final WxPayOrderNotifyResult notifyResult = wxPayConfiguration.getwxService().parseOrderNotifyResult(xmlData);
        // TODO 根据自己业务场景需要构造返回对象
        log.info("支付回调通知处理：{}", notifyResult.getOutTradeNo());

        shopUserOrderRestApi.parseOrderNotifyResult(notifyResult.getOutTradeNo());
        return "{  \n" +
                "    \"code\": \"SUCCESS\",\n" +
                "    \"message\": \"失败\"\n" +
                "}";
    }

    @ApiOperation(value = "获取微信手机号")
    @GetMapping("/getPhoneByWeChatCode")
    public ResultWrapper<?> getPhoneByWeChatCode(String code) {
        WxMaPhoneNumberInfo wxMaPhoneNumberInfo = null;
        try {
            wxMaPhoneNumberInfo = wxMaService.getUserService().getPhoneNoInfo(code);
        } catch (WxErrorException e) {
            return ResultWrapper.getCustomResultWrapper(500, e.getMessage());
        }
        return ResultWrapper.getSuccessResultWrapper(wxMaPhoneNumberInfo);
    }

}
